package com.yanqu.road.dao.impl.activity.biaoju;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.biaoju.BiaoJuUserEscortInfo;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuUserData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.CrossZhenHuanPartyData;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuUserImpl extends TempDataObjectDao<CrossBiaoJuUserData> {

    public static String getCrossBiaoJuUserDataSql = "select * from `t_c_biaoju_user` where `activityId`=? and unionGroup=?";

    public Map<Long, CrossBiaoJuUserData> getCrossBiaoJuUserData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossBiaoJuUserData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossBiaoJuUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossBiaoJuUserData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossBiaoJuUserDataSql = "update `t_c_biaoju_user` set unionId=?,unionPos=?,level=?,exp=?,expChangeTime=?,money=?,guaranteeAmount=?,lastRefreshTime=?,waitTakeList=?,rank=?,lastConfirmFinishTime=?,dayJoinNum=?,dayJoinRewardNum=?,canBePullQuality=?,settingSwitch=?,canPullId=?,unionExpAdd=?,dayUnionExpAdd=?,lastBuildTime=? where activityId=? and userId=? and unionGroup=?";
    @Override
    protected boolean updateData(CrossBiaoJuUserData crossBiaoJuUserData) {
        crossBiaoJuUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getUnionPos()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getExpChangeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getMoney()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getGuaranteeAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getLastRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserData.getWaitTakeList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getLastConfirmFinishTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getDayJoinNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getDayJoinRewardNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserData.getCanBePullQuality())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getSettingSwitch()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserData.getCanPullId())));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getUnionExpAdd()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getDayUnionExpAdd()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getLastBuildTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossBiaoJuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossBiaoJuUserDataSql = "insert into `t_c_biaoju_user`(activityId,userId,unionGroup,unionId,unionPos,level,exp,expChangeTime,money,guaranteeAmount,lastRefreshTime,waitTakeList,rank,lastConfirmFinishTime,dayJoinNum,dayJoinRewardNum,canBePullQuality,settingSwitch,canPullId,unionExpAdd,dayUnionExpAdd,lastBuildTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossBiaoJuUserData crossBiaoJuUserData) {
        crossBiaoJuUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getUnionPos()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getExpChangeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getMoney()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getGuaranteeAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getLastRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserData.getWaitTakeList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getRank()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getLastConfirmFinishTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getDayJoinNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getDayJoinRewardNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserData.getCanBePullQuality())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserData.getSettingSwitch()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserData.getCanPullId())));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getUnionExpAdd()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getDayUnionExpAdd()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserData.getLastBuildTime()));
        if (getDb().executeNoneQuery(insertCrossBiaoJuUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossBiaoJuUserData parseData(ResultSet rs) throws SQLException {
        CrossBiaoJuUserData crossBiaoJuUserData = new CrossBiaoJuUserData();
        crossBiaoJuUserData.setActivityId(rs.getInt("activityId"));
        crossBiaoJuUserData.setUserId(rs.getLong("userId"));
        crossBiaoJuUserData.setUnionGroup(rs.getInt("unionGroup"));
        crossBiaoJuUserData.setUnionId(rs.getString("unionId"));
        crossBiaoJuUserData.setUnionPos(rs.getInt("unionPos"));
        crossBiaoJuUserData.setLevel(rs.getInt("level"));
        crossBiaoJuUserData.setExp(rs.getLong("exp"));
        crossBiaoJuUserData.setExpChangeTime(rs.getLong("expChangeTime"));
        crossBiaoJuUserData.setMoney(rs.getLong("money"));
        crossBiaoJuUserData.setGuaranteeAmount(rs.getInt("guaranteeAmount"));
        crossBiaoJuUserData.setLastRefreshTime(rs.getLong("lastRefreshTime"));
        crossBiaoJuUserData.setWaitTakeList(JSON.parseObject(rs.getString("waitTakeList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.biaoju.BiaoJuUserEscortInfo>>() {}));
        crossBiaoJuUserData.setRank(rs.getInt("rank"));
        crossBiaoJuUserData.setLastConfirmFinishTime(rs.getLong("lastConfirmFinishTime"));
        crossBiaoJuUserData.setDayJoinNum(rs.getInt("dayJoinNum"));
        crossBiaoJuUserData.setDayJoinRewardNum(rs.getInt("dayJoinRewardNum"));
        crossBiaoJuUserData.setCanBePullQuality(JSON.parseObject(rs.getString("canBePullQuality"), ConcurrentHashSet.class));
        crossBiaoJuUserData.setSettingSwitch(rs.getInt("settingSwitch"));
        crossBiaoJuUserData.setCanPullId(JSON.parseObject(rs.getString("canPullId"), ConcurrentHashSet.class));
        crossBiaoJuUserData.setUnionExpAdd(rs.getLong("unionExpAdd"));
        crossBiaoJuUserData.setDayUnionExpAdd(rs.getLong("dayUnionExpAdd"));
        crossBiaoJuUserData.setLastBuildTime(rs.getLong("lastBuildTime"));

        return crossBiaoJuUserData;
    }
}
